package me.fung.rem.app.auth

import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import me.fung.keen.app.view.View
import me.fung.rem.app.common.RemTextField
import me.fung.rem.app.common.goBackButton

const val PAGE_MODIFY_PASSWORD = "page_modify_password"

class ModifyPassword : View<SignUpPresenter>() {

    @Composable
    override fun content(modifier: Modifier) {
        Box(modifier = modifier) {
            modifyContent()
            goBackButton()
        }

    }

    @OptIn(ExperimentalMaterialApi::class)
    @Composable
    fun BoxScope.modifyContent() {
        val rp = remember { p }
        val focusRequester = remember { FocusRequester() }
        LaunchedEffect(Unit) {
            focusRequester.requestFocus()
        }
        Column(
            modifier = Modifier.align(Alignment.Center).width(IntrinsicSize.Min),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            RemTextField(
                value = rp.deprecatedPwd,
                onValueChange = { rp.deprecatedPwd = it },
                modifier = Modifier.focusRequester(focusRequester).fillMaxWidth(),
                placeholder = { Text("录入旧密码") },
                visualTransformation = PasswordVisualTransformation(),
                singleLine = true,
            )
            RemTextField(
                value = rp.pwd,
                onValueChange = { rp.pwd = it },
                modifier = Modifier.fillMaxWidth(),
                placeholder = { Text("录入密码") },
                visualTransformation = PasswordVisualTransformation(),
                singleLine = true,
            )
            RemTextField(
                value = rp.repeatPwd,
                onValueChange = { rp.repeatPwd = it },
                modifier = Modifier.fillMaxWidth(),
                placeholder = { Text("重复录入密码") },
                visualTransformation = PasswordVisualTransformation(),
                singleLine = true,
            )
            CompositionLocalProvider(LocalMinimumInteractiveComponentEnforcement provides false) {
                Button(
                    onClick = { rp.modifyPwd() },
                    modifier = Modifier.fillMaxWidth().padding(5.dp)
                ) {
                    Text("修改密码")
                }
            }
        }
    }
}